package com.team1.supervend.domain;

public class PaymentHandler {
	private CashPayment currentCashPayment;
	private CreditPayment currentCreditPayment;
	float totalPayment;
	public PaymentHandler(){
		//System.out.println("Creating Payment Handler");
	}
	
	public boolean makeCashPayment( Money amount )
	{
		boolean validPayment;
		if (amount.get() > .01f){ 
			currentCashPayment = new CashPayment(amount);
			validPayment = true;
		}
		else validPayment = false;
		ackCashPayment();
		totalPayment = totalPayment + amount.get();
		System.out.println("");
		System.out.println("-------------------");
		System.out.format("Total Cash Payments: $%.2f\n", totalPayment);
		System.out.println("-------------------");
		System.out.println("");
		return validPayment;
		
	}

	public PaymentAmount ackCashPayment(){
		PaymentAmount balance;
		balance = currentCashPayment.get();
		System.out.println("");
		System.out.println("-------------------");
		System.out.format("Cash Payment added: $%.2f\n", balance.get());
		System.out.println("-------------------");
		System.out.println("");
		return balance;
	}

	public boolean makeCreditPayment( Credit amount )
	{
		IPaymentAuthorizationServiceAdapter creditPaymentService = CreditPaymentAuthorizationServiceFactory.getInstance().getPaymentAuthorizationServiceAdapter();
		boolean validPayment = false;
		if (amount.get() > .01f){ 
			currentCreditPayment = new CreditPayment(amount);
			if (creditPaymentService.preValidatePayment()){
				validPayment = true;
			}
			return validPayment;
		}
		else validPayment = false;
		ackCreditPayment();
		totalPayment = totalPayment + amount.get();
		System.out.println("");
		System.out.println("-------------------");
		System.out.format("Total Credit Payments: $%.2f\n", totalPayment);
		System.out.println("-------------------");
		System.out.println("");
		return validPayment;
		
	}
	
	public boolean makeCreditPrePayment(  )
	{
		IPaymentAuthorizationServiceAdapter creditPaymentService = CreditPaymentAuthorizationServiceFactory.getInstance().getPaymentAuthorizationServiceAdapter();
		boolean validPayment = false;
			if (creditPaymentService.preValidatePayment()){
				validPayment = true;
			}
			return validPayment;
	}
	
	public PaymentAmount ackCreditPayment(){
		PaymentAmount balance;
		balance = currentCreditPayment.get();
		System.out.println("");
		System.out.println("-------------------");
		System.out.format("Credit Payment added: $%.2f\n", balance.get());
		System.out.println("-------------------");
		System.out.println("");
		return balance;
	}

}
